import wxutils from "./wxutils";
import gamedata from "./gamedata";
import logger from "./logger";

export default class netMgr {
    private static _Instance: netMgr;
    public xhr: Laya.HttpRequest;
    // public baseUrl = "http://114.116.248.80:8080/";
    public baseUrl = 'https://api.fzrpg.com/';
    static get Instance(): netMgr {
        if (!this._Instance)
            this._Instance = new netMgr();
        return this._Instance;
    }
    constructor() {
        this.xhr = new Laya.HttpRequest();
        this.xhr.http.timeout = 10000;//设置超时时间；
        // this.xhr.
        // console.log("aaaa");
    }

    //get方法 
    public get(url, method) {
        this.xhr.once(Laya.Event.COMPLETE, this, this.completeHandler, [method]);
        this.xhr.once(Laya.Event.ERROR, this, this.errorHandler);
        this.xhr.on(Laya.Event.PROGRESS, this, this.processHandler);
        this.xhr.send(url, "", "get", "json", ['openid', gamedata.Instance.openid, 'appname', 'mibao']);
    }

    public post(url, data, method) {
        let headers = []
        if (gamedata.Instance.openid) {
            headers = [{ openid: gamedata.Instance.openid, appname: 'mibao' }]
        }
        this.xhr.once(Laya.Event.COMPLETE, this, this.completeHandler, [method]);
        this.xhr.once(Laya.Event.ERROR, this, this.errorHandler);
        this.xhr.on(Laya.Event.PROGRESS, this, this.processHandler);
        this.xhr.send(url, data, "post", "json", ['openid', gamedata.Instance.openid, 'appname', 'mibao']);

    }

    private processHandler(data: any): void {
        console.log(data);
    }
    private errorHandler(data: any): void {
    }
    //请求 结束 回调 事件
    private completeHandler(method: any, e?: any): void {
        // console.error("====>",JSON.stringify(this.xhr.data))
        // Laya.Handler.create(this, method).runWith(this.xhr.data);
        if (typeof e === 'undefined') {
            Laya.Handler.create(this, method).run();
        } else {
            Laya.Handler.create(this, method).runWith(e);
        }
        // console.log("返回值", e)
    }













    // /Address
    public getAdress() {
        this.get(this.baseUrl + Apis.getAdress, (res) => {
            logger.log("请求收货地址", res);
        })
    }


    public setAdress() {//名字长度16位  电话长度16  地址128
        let url = netMgr.Instance.baseUrl + Apis.getAdress;
        let data = { Name: "asd", Detail: "12312", Phone: "17741773451" }
        let xhr = new Laya.HttpRequest();
        // let headers = [{ openid: gamedata.Instance.openid, appname: 'mibao' }]
        xhr.once(Laya.Event.COMPLETE, this, this.completeHandler, [(res) => {
            console.log("请求地址返回", res)
        }]);
        xhr.once(Laya.Event.ERROR, this, this.errorHandler);
        xhr.on(Laya.Event.PROGRESS, this, this.processHandler);
        xhr.send(url, data, "post", "json", ['openid', gamedata.Instance.openid, 'appname', 'mibao']);
    }


    public luckDraw() {
        // /LuckyDraw
        this.get(this.baseUrl + Apis.choujiang, (res) => {
            logger.log("抽奖：", res);
        })

    }



}


export enum Apis {
    PeiZhi = 'data/data.json',
    Heartbeat = "Heartbeat",
    Login = 'Login',
    UserInfo = 'UserInfo',
    getAdress = 'Address',
    choujiang = 'LuckyDraw',
}
